package com.tdkj.action.front.selfadvert;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import com.tdkj.action.BaseAction;
import com.tdkj.model.friendlylink.BeanFriendlylink;
import com.tdkj.model.selfadvert.BeanSelfAdvert;
import com.tdkj.model.user.BeanAdminLevel;
import com.tdkj.model.user.BeanUser;
import com.tdkj.service.friendlylink.IBeanFriendlylinkService;
import com.tdkj.service.selfadvert.IBeanSelfAdvertService;


public class SelfAdvertAction extends BaseAction {
	private Long id;
	private String type;
	private BeanSelfAdvert advert;
	private List<BeanSelfAdvert> adverttexts=new ArrayList<BeanSelfAdvert>();
	private List<BeanSelfAdvert> advertimgs=new ArrayList<BeanSelfAdvert>();
	private IBeanSelfAdvertService beanSelfAdvertService;
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public BeanSelfAdvert getAdvert() {
		return advert;
	}
	public void setAdvert(BeanSelfAdvert advert) {
		this.advert = advert;
	}
	public List<BeanSelfAdvert> getAdverttexts() {
		return adverttexts;
	}
	public void setAdverttexts(List<BeanSelfAdvert> adverttexts) {
		this.adverttexts = adverttexts;
	}
	public List<BeanSelfAdvert> getAdvertimgs() {
		return advertimgs;
	}
	public void setAdvertimgs(List<BeanSelfAdvert> advertimgs) {
		this.advertimgs = advertimgs;
	}
	public IBeanSelfAdvertService getBeanSelfAdvertService() {
		return beanSelfAdvertService;
	}
	public void setBeanSelfAdvertService(
			IBeanSelfAdvertService beanSelfAdvertService) {
		this.beanSelfAdvertService = beanSelfAdvertService;
	}
	public String findAll(){
		adverttexts=beanSelfAdvertService.findTextList();
		advertimgs=beanSelfAdvertService.findImgList();
		if(adverttexts.size()==0){appendObj(10,"1");}
		if(advertimgs.size()==0){appendObj(10,"2");}
		adverttexts=beanSelfAdvertService.findTextList();
		advertimgs=beanSelfAdvertService.findImgList();
		return "findAll";
	}
	public String findTextList(){
		adverttexts=beanSelfAdvertService.findTextList();
		if(adverttexts.size()==0){appendObj(10,"1");}
		return "findTextList";
	}
	public String findImgList(){
		advertimgs=beanSelfAdvertService.findImgList();
		if(advertimgs.size()==0){appendObj(10,"2");}
		return "findImgList";
	}
	public String findTextContent(){
		advert=beanSelfAdvertService.findById(this.getId(),BeanSelfAdvert.class);
		return "findTextContent";
	}
	public String findImgContent(){
		advert=beanSelfAdvertService.findById(this.getId(),BeanSelfAdvert.class);
		return "findImgContent";
	}
	public String findAddPage(){
		String type=this.getType();
		if(type.equals("2")){
			return "img_addPage";
		}else{
			return "text_addPage";
		}
	}
	public String addSelfAdvert(){
		BeanSelfAdvert bean=new BeanSelfAdvert();
		bean=this.getAdvert();
		bean.setCreatetime(new Timestamp(System.currentTimeMillis()));
		String type=bean.getAdverttype();
		beanSelfAdvertService.save(bean);
		if(type.equals("2")){
			return "img_addPage";
		}else{
			return "text_addPage";
		}
	}
	
	private  void appendObj(int num,String type) {
		// TODO Auto-generated method stub
		BeanSelfAdvert bean=null;
		for(int i=0;i<num;i++){
			bean=new BeanSelfAdvert();
			bean.setEnterpricename("中华科技有限公司");
			bean.setEmail("zk@345.com");
			bean.setLinkman("张封魔");
			bean.setTitle("好东西"+i);
			bean.setContent("好东西的内容展示"+i);
			bean.setFax("123-098765");
			BeanUser u=beanSelfAdvertService.findById(5, BeanUser.class);
			bean.setUser(u);
			bean.setStatus("未审核");
			if(type.equals("1")){
				bean.setAdverttype("1");
			}
			if(type.equals("2")){bean.setAdverttype("2");bean.setImgurl("images/"+(i+1)+".png");}
			bean.setCreatetime(new Timestamp(System.currentTimeMillis()));
			beanSelfAdvertService.save(bean);
		}
	}
}
